<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends CI_Controller {
    var $datah, $ids, $role ;
        
public function __construct()
	{
            parent::__construct();
            $this->load->helper('url');
            $this->load->model('menu_model');
            $this->datah['baseurl'] = base_url();
            
            session_start();
            $role = 'public';   // predefine the var 
            if (isset($_SESSION)) {
             if ( isset($_SESSION['logged_in'])) {
                $role = $_SESSION['role']; } else { $role = 'public'; } } 
            
            $this->datah['role'] = $role;   
            $this->datah['menu'] = $this->menu_model->getmenu( $role ,'users' );
          //  $this->ids['menu'] = $this->menu_model->getmenu( $role ,'users' );
          //  $this->ids['base'] = base_url();
             // LOAD LIBRARIES
            $this->load->library(array('encrypt', 'form_validation'));

            // LOAD HELPERS
            $this->load->helper(array('form', 'array' ));
            $this->datah['showmenu'] = TRUE;
	}
        
        public function menulist() {
            if( $this->input->post('filt') )  // is the filter button set
            {
                $mfilt = $this->input->post('filter');
            } else {
                $mfilt = 'all';
            }
            // var_dump($mfilt);
            $grid['menu'] = $this->menu_model->menulist($mfilt);
            //var_dump($grid['menu']);
            $grid['toplevel'] = $this->menu_model->menutoplevel();
            $grid['gridtype'] = 'menu';
            $grid['baseurl'] = $this->datah['baseurl'];
            $data['title'] = ucfirst('Users'); // Capitalize the first letter
            $this->datah['showmenu'] = TRUE;
            $this->load->view('templates/header', $this->datah);
            $this->load->view('admin/Listgrid', $grid);
            $this->load->view('templates/footer');
        }
        
        public function modmenu($mid = NULL) {
           $menu['url'] = 'index.php/modmenu/'.$mid;
           
           $menu['toplevel'] = $this->menu_model->menutoplevel();  # needed to select a top level menu
           if ($mid !== 'new') {  // if it is not set the we are doing an insert
              $data = $this->menu_model->getmenubyid($mid);
              // strip out role
              $roleval = $this->arraytotextarea($data[0]['roles']);
              $tagsval = $this->arraytotextarea($data[0]['tags']);
              $pagesval = $this->arraytotextarea($data[0]['pages']);
              
              $menu['menud'] = array( "id" => $mid, "name" => $data[0]['name'], "url" => $data[0]['url'], "parent" => $data[0]['parent'], "tags" => $tagsval , "roles" => $roleval , "pages" => $pagesval ); 
              $menu['heading'] = 'Modfy Menu Item';
              $menu['type'] = 'edit';
           } else {
              // create a new menu
              $menu['menud'] = array( "id" => $mid, "name" => "", "url" => "", "parent" => "", "tags" => "", "roles" => "" , "pages" => "" ); 
              $menu['heading'] = 'New Menu Item';
              $menu['type'] = 'new';
           }
           
           $this->setvalidmenu();
           
           if( $this->input->post('save') ) { // is the filter button set
               if( $this->form_validation->run() == TRUE ) {
                   // validation ok
                   $postdata = $this->input->post(NULL, TRUE);
                   $updata = array( "name" => $this->input->post('tname'), 
                       "url" => $this->input->post('url'), 
                       "parent" => $this->input->post('parent'), 
                       "tags" => $this->txt2array($this->input->post('tags')), 
                       "roles" => $this->txt2array($this->input->post('roles')) , 
                       "pages" => $this->txt2array($this->input->post('pages')) );
                   if ($this->input->post('type') == 'new') {
                       $this->menu_model->insertmenu($updata);
                   } else {
                        $this->menu_model->updatemenu($mid, $updata);
                   }     
                   redirect(base_url().'index.php/menulist');
               } else {
                  // Validation Failed 
                   //echo "Validation Failed";
                   $this->load->view('templates/header', $this->datah);
                   $this->load->view('admin/updatemenu', $menu);
                   $this->load->view('templates/footer');
               }
           } else {
                // no post
                $this->load->view('templates/header', $this->datah);
                $this->load->view('admin/updatemenu', $menu);
                $this->load->view('templates/footer');
            }
            
        }
        
        public function addarrayitem( $data ) {
            // $data = array(id, itemtoadd, array2addto)
            var_dump($data);
            $isok = $this->menu_model->addpage2menu($data[0], $data[1], $data[2]);
            echo $isok;
            
        }
        function setvalidmenu() {
            $this->form_validation->set_rules('tname', 'Menu Name', 'required|min_length(5)');
//            $this->form_validation->set_rules('street', 'Street Name', 'required|min_length(3)');
//            $this->form_validation->set_rules('roadnum', 'Number', 'required||min_length(1)');
//            $this->form_validation->set_rules('intersect', 'Intersection', 'required||min_length(5)');
//            $this->form_validation->set_rules('longitude', 'Longitude', 'greater_than(-179)|less_than(181)|numeric');
//            $this->form_validation->set_rules('latitude', 'Latitude', 'greater_than(-179)|less_than(181)|numeric');
//            $this->form_validation->set_rules('accepttandc', 'Terms and Conditions', 'required');
//            $this->form_validation->set_rules('tenanttype', 'Terms and Conditions', 'required|alpha');
            $this->form_validation->set_error_delimiters('<em>','</em>');
       }
       
       function arraytotextarea ($arrat2convert) {
           $chrlf = "\n" ; //chr(13);
           $result = "";
              if ($arrat2convert !== FALSE ) {
                foreach($arrat2convert as $value)
                {
                    $result = $result . $value . $chrlf;
                  //echo $roleval;
                }  
              }
              return $result;
       }
       
       function txt2array ($txt2convert) {
            
            //$text = trim($txt2convert);
            $textAr = explode("\n", $txt2convert);
            return $textAr;
            //$textAr = array_filter($text, 'trim');
       }
}
?>
